package com.framework.cat.controller;/**
 * @author dongsufeng
 * @date 2019/9/27 17:51
 * @version 1.0
 */

import com.dianping.cat.Cat;
import com.framework.cat.service.OrderService;
import lombok.extern.log4j.Log4j2;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;

/**
 *
 * @author dongsufeng
 * @date 2019/9/27 17:51
 * @version 1.0
 */
@RestController
@Log4j2
public class CatController {
	@Autowired
	RestTemplate restTemplate;
	@DubboReference(timeout = 3000)
	private OrderService orderService;


	@PostMapping("cat-http1")
	public String call(HttpServletRequest request){
		log.info("===================cat-http1");
		String order00001 = null;
		try {
			order00001 = orderService.getOrder("order00001");
		} catch (Exception e) {
		}
		try {
			String body="{'code':'0000'}";
			HttpEntity<String> entity = new HttpEntity<>(body);
			ResponseEntity<String> exchange = restTemplate.exchange("http://localhost:8001/cat-http2",
					HttpMethod.POST, entity, String.class);
			log.info(exchange.getBody());
		} catch (Exception e) {

		}
		return "http1=="+order00001;
	}
	@PostMapping("cat-http1-error")
	public String httpError(){
		log.info("===================cat-http1-error");
		try {
			int i=1/0;
		} catch (Exception e) {
			Cat.logError(e);
		}
		return "http1-error";
	}
}
